//
//  DecimalPointButton.m
//  iDeal
//
//  Created by David Casserly on 13/03/2010.
//  Copyright 2010 devedup.com. All rights reserved.
//

#import "NumberKeypadDecimalPoint.h"

@implementation DecimalPointButton


- (id) init {
	if(self = [super initWithFrame:CGRectMake(0, 480, 105, 53)]) { //Initially hidden	
		//[super adjustsImageWhenDisabled:NO];
		self.titleLabel.font = [UIFont systemFontOfSize:35];
		[self setTitleColor:[UIColor colorWithRed:77.0f/255.0f green:84.0f/255.0f blue:98.0f/255.0f alpha:1.0] forState:UIControlStateNormal];
        UIImage* backgroundImageDepressed = [UIImage imageNamed:@"decimalKeyDownBackground.png"];
		[self setBackgroundImage:backgroundImageDepressed forState:UIControlStateHighlighted];
        [self setBackgroundImage:[UIImage imageNamed:@"decimalKeyDownBackground_1.PNG"] forState:UIControlStateNormal];
		[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
		[self setTitle:@".。。" forState:UIControlStateNormal];
        self.backgroundColor = [UIColor redColor];
    }
	return self;
}

- (void)drawRect:(CGRect)rect {
	[super drawRect:rect];
	
	//Bring in the button at same speed as keyboard在键盘弹出时，让小数点也往上弹出
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.2]; //we lose 0.1 seconds when we display it with timer
	self.frame = CGRectMake(0, 427, 105, 53);
	[UIView commitAnimations];
}

+ (DecimalPointButton *) decimalPointButton {
	DecimalPointButton *button = [[DecimalPointButton alloc] init];
	return [button autorelease];
}

@end

/**
 *
 */
@implementation NumberKeypadDecimalPoint

static NumberKeypadDecimalPoint *keypadlll;

//Retain
@synthesize decimalPointButton;
@synthesize showDecimalPointTimer;
//Assign
@synthesize currentTextField;

#pragma mark -
#pragma mark Release

- (void) dealloc {
	[decimalPointButton release];
	[showDecimalPointTimer release];
	[super dealloc];
}

- (id) init {
	if(self = [super init]) { 
		//[super adjustsImageWhenDisabled:NO];
        self.decimalPointButton = [DecimalPointButton decimalPointButton];
		[self.decimalPointButton addTarget:self action:@selector(clickDecimalPoint) forControlEvents:UIControlEventTouchUpInside];}
	return self;
}

#pragma mark- Private
//一段延迟后 被执行   添加button到键盘的窗口
//Private Method //This is executed after a delay from showKeypadForTextField
- (void) _addTheDecimalPointToKeyboard {
	//[keypad addButtonToKeyboard:keypad.decimalPointButton];
    NSArray *allWindows = [[UIApplication sharedApplication] windows];

	int topWindow = [allWindows count] - 1;
	UIWindow *keyboardWindow = [allWindows objectAtIndex:topWindow];
	[keyboardWindow addSubview:self.decimalPointButton];

}

#pragma mark- Private
//Private Method 点击小数点后的行为。
- (void) clickDecimalPoint {
    
    //个人理解 应该回调正常的delegate，使键盘的函数统一，避免同一功能出现多次
    NSRange range = NSMakeRange(currentTextField.text.length, 1);
    if ([self.currentTextField.delegate textField:currentTextField shouldChangeCharactersInRange:range replacementString:@"."] == YES)
    {
        currentTextField.text = [currentTextField.text stringByAppendingString:@"."];
    }
}

/*
 Show the keyboard  准确的理解应该是 添加小数点到键盘上，而非显示键盘
 */
+ (void) addTheDecimalPointToTextField:(UITextField *)textField {
    //是单例模式，可以写成规范的单利模式
	if (!keypad) {
		keypadlll = [[NumberKeypadDecimalPoint alloc] init];
	}
	keypad.currentTextField = textField;
    //一段延迟后添加小数点button 到当前键盘   猜测：延迟的目的是为了和键盘弹出时的延迟同步 通过（经过测试，猜测正确，但是可能还有其他原因，因为第一次点击键盘时 没出现小数点）结论：是因为键盘出现是异步的，有延迟，此时，把button加到最顶层的窗口上（键盘所在窗口）。如果太早添加的话，此时键盘的窗口还没有添加。
    //[keypad addTheDecimalPointToKeyboard];
	keypadlll.showDecimalPointTimer = [NSTimer timerWithTimeInterval:0.1 target:keypad selector:@selector(_addTheDecimalPointToKeyboard) userInfo:nil repeats:NO];
	[[NSRunLoop currentRunLoop] addTimer:keypad.showDecimalPointTimer forMode:NSDefaultRunLoopMode];
	
}

/*
 Hide the keyboard  准确的理解是 移除小数点。
 */
+ (void) removeButtonFromKeyboard {
	[keypad.showDecimalPointTimer invalidate]; //stop any timers still wanting to show the button
	[keypad.decimalPointButton removeFromSuperview];
}


@end

